package net.monare.mifss.evn;

import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.mod.ApiKeyUiModule;
import net.monare.mifss.admin.ui.win.ConfigureApiKeyWindow;
import net.monare.mifss.admin.ui.win.ConfigureStorageWindow;
import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.StorageServiceException;


import com.vaadin.ui.Notification;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;



public class UpdateApiKeyListener extends BasicMifssListener implements ClickListener {

	private static final long serialVersionUID = -7200140258523955787L;

	private ApiKeyUiModule apiKeyUiModule;
	private StorageDef storageDef;
	private ConfigureStorageWindow parent;
    private ConfigureApiKeyWindow parentWindow;



	public UpdateApiKeyListener(StorageDef storageDef, ApiKeyUiModule apiKeyUiModule, ConfigureStorageWindow parent, ConfigureApiKeyWindow parentWindow) {

		this.apiKeyUiModule = apiKeyUiModule;
		this.storageDef = storageDef;
		this.parent = parent;
        this.parentWindow = parentWindow;
	}

	public void buttonClick(ClickEvent event) {

		try {

			ApiKey apiKey = apiKeyUiModule.getApiKey();

			if (apiKey != null) {

				getMifssHolder().getStorageService().modifyApiKey(storageDef.getName(), apiKey);
				parent.resetAllPanes();
				Notification.show(MifssMessages.getMessage("sys.success.api.key.updated"));
				parentWindow.close();

			} else {

				Notification.show(MifssMessages.getMessage("sys.error.api.key.not.defined"));

			}

		} catch (StorageServiceException e) {
			Notification.show(MifssMessages.getMessage("sys.error.api.key.update") + " : " + e.getMessage());
		}

	}

}
